/**  
 * @Copyright：中科软科技股份有限公司——行业推广一部 
 * @ProjectName：sinosoftHelp 
 * @Title: StringBizUtil.java
 * @Package: com.sinosoft.sinosoftHelp.util.string
 *  
 * @author: lixl  
 * @date: 2014年4月16日 下午2:22:17
 * @version: V1.0  
 */
package com.sinosoft.jwjw.utils;

import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  字符串操作的工具类
 * @ClassName：com.sinosoft.sinosoftHelp.util.string.StringBizUtil
 * @author: lixl
 * @date: 2014年4月16日 下午2:22:17
 * @version: V1.0er
 * 
 */

public class StringUtil {

	private static String[] chineseDigits = new String[] { "零", "壹", "贰", "叁",
			"肆", "伍", "陆", "柒", "捌", "玖" };
	private static final char[] zeroArray = "0000000000000000".toCharArray();

	/**
	 * 得到字符串的长度（汉字两个字符长度）
	 * @Title: getLength
	 * @author: lixl  
	 * @date: 2014年4月18日 下午4:49:26 
	 * @param s 输入的字符串
	 * @return 字符串的长度
	 */
	public static int getLength(String s) {
		if (isNull(s) && !"null".equals(s)) {
			return 0;
		}
		int len = 0;
		int code = 0;
		for (int i = 0; i < s.length(); i++) {
			code = s.charAt(i); // s.charCodeAt(i);
			len += (code >= 0 && code <= 255 ? 1 : 2);
		}
		return len;
	}

	/**
	 *  判断字符串是否为空：空包括 null "" "null" "undefined"
	 * @Title: isEmpty
	 * @author: lixl  
	 * @date: 2014年4月16日 下午3:42:52 
	 * @param s 需要判断的字符串
	 * @return boolean
	 */

	public static boolean isEmpty(String s) {
		if (s == null || s.trim().equals("") || s.trim().equals("null")
				|| s.trim().equals("undefined")) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符串是否为空：空包括：null、""、"null"
	 * @Title: isNull
	 * @author: lixl  
	 * @date: 2014年4月16日 下午5:57:04 
	 * @param s 需要判断的字符串
	 * @return 返回的字符串
	 */
	public static boolean isNull(String s) {
		if (s == null || s.trim().equals("") || s.trim().equals("null")) {
			return true;
		}
		return false;
	}

	/**
	 * <p>查找是否存在</p>    
	 * @param str
	 * @return
	 * @throws Exception <br><br>
	 * author jzhong<BR>
	 * date 2011-12-29<br>
	 * remark <br>
	 */
	public static boolean indexOf(String str, String key) {
		return !isEmpty(str) && str.indexOf(key) > 1;
	}

	/**
	 * 查找字符串是否存在
	 * @Title: indexOf
	 * @author: lixl  
	 * @date: 2014年4月16日 下午4:05:48 
	 * @param str 需要查找的字符串
	 * @param keys 需要查找的源字符串
	 * @return boolean
	 */
	public static boolean indexOf(String str, String[] keys) {
		boolean isMore = false;

		if (!isEmpty(str) && !isEmpty(keys)) {
			for (String key : keys) {
				if (str.indexOf(key) > 1) {
					return !isMore;
				}
			}
		}

		return isMore;
	}

	/**
	 *  比较两个字符串是否相等，扩展的功能是进行了空的判断
	 * @Title: equals
	 * @author: lixl  
	 * @date: 2014年4月16日 下午4:12:21 
	 * @param a 需要判断的字符串
	 * @param b 需要判断的字符串
	 * @return
	 */
	public static boolean equals(String a, String b) {
		boolean isEquals = false;

		if (!isEmpty(a) && !isEmpty(b)) {
			if (a.equals(b)) {
				return !isEquals;
			}
		}
		return isEquals;
	}

	/**
	 *  判断数组中是否包含字符串 区分大小写
	 * @Title: in
	 * @author: lixl  
	 * @date: 2014年4月16日 下午4:19:07 
	 * @param str
	 * @param strs
	 * @return boolan
	 */
	public static boolean in(String str, String[] strs) {
		boolean isHas = false;
		if (!isEmpty(str) && !isEmpty(strs)) {
			for (String inStr : strs) {
				if (str.equals(inStr)) {
					return !isHas;
				}
			}
		}
		return isHas;
	}

	/**
	 * 判断字符串内容是否为数字
	 * @Title: isDigital
	 * @author: lixl  
	 * @date: 2014年4月16日 下午4:31:33 
	 * @param str 需要判断的字符串
	 * @return boolean
	 */
	public static boolean isDigital(String str) {
		return Pattern.compile("^[0-9]*$").matcher(str).matches();
	}

	/**
	 * 把一个字符串字符转换数字 整数类型
	 * @Title: toDigital
	 * @author: lixl  
	 * @date: 2014年4月16日 下午4:32:25 
	 * @param str  需要转换的字符串
	 * @param defaultValue 默认值
	 * @return
	 */
	public static int toDigital(String str, int defaultValue) {
		if (isDigital(str)) {
			return Integer.parseInt(str);
		}

		return defaultValue;
	}

	/**
	 * 判断字符串数组中是否有空格
	 * @Title: isEmpty
	 * @author: lixl  
	 * @date: 2014年4月16日 下午3:47:00 
	 * @param ss 需要判断的字符串数组
	 * @return 返回boolean
	 */
	public static boolean isEmpty(String[] ss) {
		if (ss == null)
			return true;
		for (String s : ss) {
			if (isEmpty(s))
				return true;
		}

		return false;
	}

	/**
	 * 属性转换成表字段，例如:makeDate->make_Date
	 * @Title: attributeToFiled 
	 * @author: lixl  
	 * @date: 2014年4月16日 下午2:45:18 
	 * @param attribute 需要转换的属性名称
	 * @return
	 */
	
	
	public static String attributeToFiled(String attribute) {
		// if(attribute==""||attribute==null){
		if (StringUtil.isNull(attribute)) {
			return "";
		}
		StringBuilder sb = new StringBuilder(attribute);
		String temp = null;
		int k = 0;
		for (int i = 0; i < attribute.length(); i++) {
			if (Character.isUpperCase(attribute.charAt(i))) {// 如果当前字符是大写
				temp = "_" + attribute.charAt(i);
				sb.replace(i + k, i + k+1, temp); // 在字符串i这个位置替换temp
				k++;
			}
		}
		return sb.toString();
	}
	
	 /** 
     * 字段转换成对象属性 例如：user_name to userName 
     * @param field 
     * @return 
     */  
    public static String fieldToProperty(String field) {  
        if (null == field) {  
            return "";  
        }  
        char[] chars = field.toCharArray();  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < chars.length; i++) {  
            char c = chars[i];  
            if (c == '_') {  
                int j = i + 1;  
                if (j < chars.length) {  
                    sb.append((chars[j]+"").toUpperCase(Locale.CHINA));  
                    i++;  
                }  
            } else {  
                sb.append(c);  
            }  
        }  
        return sb.toString();  
    }  
	/**
	 * 根据对象返回表的名称
	 * @param tablename
	 * @return
	 */
	public  static String attributeToTableName(String tablename){
		tablename=  StringUtil.attributeToFiled(tablename);
	    tablename=tablename.substring(1, tablename.length()).toUpperCase(Locale.CHINA);
	    return tablename;
	}
	
	 

	/**
	 * 把一个字符串中的大写字母转换成小写，小写字母转换成大写
	 * @Title: changeUpAndLower
	 * @author: lixl  
	 * @date: 2014年4月16日 下午2:46:13 
	 * @param s 需要转换的字符串
	 * @return 返回转换后的字符串
	 */
	public static String changeUpAndLower(String s) {
		// if(s==""||s==null)
		if (StringUtil.isNull(s))
			return "";
		StringBuilder sb = new StringBuilder(s);
		String temp = null;
		for (int i = 0; i < s.length(); i++) {
			if (Character.isLowerCase(sb.charAt(i)))// 如果当前字符是小写
			{
				temp = s.substring(i, i + 1).toUpperCase();// 把i这个位置小写的字母变成大写并放进temp
				sb.replace(i, i + 1, temp); // 在字符串i这个位置替换temp
			} else if (Character.isUpperCase(sb.charAt(i))) {
				temp = s.substring(i, i + 1).toLowerCase();
				sb.replace(i, i + 1, temp);
			}
		}
		return sb.toString();
	}

	/**
	 * 功能：将一个含有数字，字母、中文及特殊字符的字符转换为数字
	 * 				注：首字母不是数字的字符返回null
	 * @Title: changeStringToNumber
	 * @author: lixl  
	 * @date: 2014年4月16日 下午2:47:25 
	 * @param str
	 * @return
	 */
	public static String changeStringToNumber(String str) {
		String str2 = null;
		if (str.matches("[0-9]+")) { // 编号全为数字
			return str;
		} else if (str.indexOf("-") != -1) { // 编号包含分隔符
			str2 = str.substring(0, str.indexOf("-")); // 编号截取到第一个分隔符位置后剩下的字符串
			if (str2.matches("[0-9]+")) { // 假如去掉分隔符及后面所有字符串后任然是数字
				return str2;
			}
		} else if (str.substring(0, 1).matches("[0-9]")) {// 编号首字母为数字
			String regex1 = "[`~!@#$%^&*()+=|{}_\\-\\':;,\\[\\].<>/?~！@#￥%……&*（）——+|{}【】《》‘；：”“’。，、？]"; // 过滤特殊字符的正则
			String regex2 = "[^x00-xff]"; // 过滤双字节字符的正则，包括汉字
			String regex3 = "[a-zA-Z]";
			str2 = str.replaceAll(regex1, "");// 去特殊字符
			str2 = str2.replaceAll(regex2, ""); // 去汉字及双字节字符
			str2 = str2.replaceAll(regex3, ""); // 去字母
			if (str2.matches("[0-9]+")) { // 假如去掉所有特殊字符及汉字后任然是数字
				return str2;
			}
		}
		return null;
	}

	/**
	 * 把金额转换为阿拉伯数字的金额
	 * @Title: changeToBig
	 * @author: lixl  
	 * @date: 2014年4月16日 下午2:47:50 
	 * @param amount
	 * @return 返回字符串类型的阿拉伯数字
	 */
	public static String changeToBig(double amount) {
		if (amount > 99999999999999.99 || amount < -99999999999999.99)
			throw new IllegalArgumentException(
					"参数值超出允许范围 (-99999999999999.99 ～ 99999999999999.99)!");

		boolean negative = false;
		if (amount < 0) {
			negative = true;
			amount = amount * (-1);

		}
		long temp = Math.round(amount * 100);
		int numFen = (int) (temp % 10); // 分
		temp = temp / 10;
		int numJiao = (int) (temp % 10); // 角
		temp = temp / 10;
		// temp 目前是金额的整数部分

		int[] parts = new int[20]; // 其中的元素是把原来金额整数部分分割为值在 0~9999 之间的数的各个部分
		int numParts = 0; // 记录把原来金额整数部分分割为了几个部分（每部分都在 0~9999 之间）
		for (int i = 0;; i++) {
			if (temp == 0)
				break;
			int part = (int) (temp % 10000);
			parts[i] = part;
			numParts++;
			temp = temp / 10000;
		}

		boolean beforeWanIsZero = true; // 标志"万"下面一级是不是 0

		String chineseStr = "";
		for (int i = 0; i < numParts; i++) {

			String partChinese = partTranslate(parts[i]);
			if (i % 2 == 0) {
				if ("".equals(partChinese))
					beforeWanIsZero = true;
				else
					beforeWanIsZero = false;
			}
			if (i != 0) {
				if (i % 2 == 0)
					chineseStr = "亿" + chineseStr;
				else {
					// 如果"万"对应的part 为 0，而"万"下面一级不为 0，则不加"万"，而加"零"
					if ("".equals(partChinese) && !beforeWanIsZero)
						chineseStr = "零" + chineseStr;
					else {
						// 如果"万"的部分不为 0,而"万"前面的部分小于 1000 大于 0，则万后面应该跟"零"
						if (parts[i - 1] < 1000 && parts[i - 1] > 0)
							chineseStr = "零" + chineseStr;
						chineseStr = "万" + chineseStr;
					}
				}
			}
			chineseStr = partChinese + chineseStr;
		}

		if ("".equals(chineseStr)) // 整数部分为 0, 则表达为"零元"
			chineseStr = chineseDigits[0];
		else if (negative) // 整数部分不为 0, 并且原金额为负数
			chineseStr = "负" + chineseStr;

		chineseStr = chineseStr + "元";

		if (numFen == 0 && numJiao == 0) {
			chineseStr = chineseStr + "整";
		} else if (numFen == 0) { // 0 分，角数不为 0
			chineseStr = chineseStr + chineseDigits[numJiao] + "角";
		} else { // "分"数不为 0
			if (numJiao == 0)
				chineseStr = chineseStr + "零" + chineseDigits[numFen] + "分";
			else
				chineseStr = chineseStr + chineseDigits[numJiao] + "角"
						+ chineseDigits[numFen] + "分";
		}

		return chineseStr;

	}

	/**
	 *  把一个 0~9999 之间的整数转换为汉字的字符串，如果是 0 则返回 ""
	 * @Title: partTranslate
	 * @author: lixl  
	 * @date: 2014年4月16日 下午2:48:53 
	 * @param amountPart
	 * @return
	 */
	private static String partTranslate(int amountPart) {
		if (amountPart < 0 || amountPart > 10000) {
			throw new IllegalArgumentException("参数必须是大于等于 0，小于 10000 的整数！");
		}
		String[] units = new String[] { "", "拾", "佰", "仟" };
		int temp = amountPart;
		String amountStr = new Integer(amountPart).toString();
		int amountStrLength = amountStr.length();
		boolean lastIsZero = true; // 在从低位往高位循环时，记录上一位数字是不是 0
		String chineseStr = "";
		for (int i = 0; i < amountStrLength; i++) {
			if (temp == 0) // 高位已无数据
				break;
			int digit = temp % 10;
			if (digit == 0) { // 取到的数字为 0
				if (!lastIsZero) // 前一个数字不是 0，则在当前汉字串前加"零"字;
					chineseStr = "零" + chineseStr;
				lastIsZero = true;
			} else { // 取到的数字不是 0
				chineseStr = chineseDigits[digit] + units[i] + chineseStr;
				lastIsZero = false;
			}
			temp = temp / 10;
		}
		return chineseStr;
	}

	

	/**
	 * 对输入参数中的敏感字符进行过滤替换,防止用户利用JavaScript等方式输入恶意代码
	 * @Title: htmlEscape HTML字符转义
	 * @author: lixl  
	 * @date: 2014年4月18日 上午10:24:44 
	 * @param input 输入的字符串
	 * @return string 转义之后的字符串
	 */
	public static String htmlEscape(String s) {
		if (isNull(s)) {
			return "";
		}
		int length = s.length();
		int newLength = length;
		boolean someCharacterEscaped = false;
		// first check for characters that might
		// be dangerous and calculate a length
		// of the string that has escapes.
		for (int i = 0; i < length; i++) {
			char c = s.charAt(i);
			int cint = 0xffff & c;
			if (cint < 32) {
				switch (c) {
				case '\r':
				case '\n':
				case '\t':
				case '\f': {
				}
					break;
				default: {
					newLength -= 1;
					someCharacterEscaped = true;
				}
				}
			} else {
				switch (c) {
				case '\"': {
					newLength += 5;
					someCharacterEscaped = true;
				}
					break;
				case '&':
				case '\'': {
					newLength += 4;
					someCharacterEscaped = true;
				}
					break;
				case '<':
				case '>': {
					newLength += 3;
					someCharacterEscaped = true;
				}
					break;
				}
			}
		}
		if (!someCharacterEscaped) {
			// nothing to escape in the string
			return s;
		}
		StringBuffer sb = new StringBuffer(newLength);
		for (int i = 0; i < length; i++) {
			char c = s.charAt(i);
			int cint = 0xffff & c;
			if (cint < 32) {
				switch (c) {
				case '\r':
				case '\n':
				case '\t':
				case '\f': {
					sb.append(c);
				}
					break;
				default: {
					// Remove this character
				}
				}
			} else {
				switch (c) {
				case '\"': {
					sb.append("&quot;");
				}
					break;
				case '\'': {
					sb.append("&#39;");
				}
					break;
				case '&': {
					sb.append("&amp;");
				}
					break;
				case '<': {
					sb.append("&lt;");
				}
					break;
				case '>': {
					sb.append("&gt;");
				}
					break;
				default: {
					sb.append(c);
				}
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 高效的字符串分割的方法，并做了trim操作
	 * 如：
	 * StringHelper.split("1-2-3", "-");<br>
	 * result: {"1","2","3"}<br>
	 * StringHelper.split("-1--2-", "-");<br>
	 * result: {"","1","","2",""}<br>
	 * StringHelper.split("123", "");<br>
	 * result: {"123"}<br>
	 * StringHelper.split("1-2---3----4", "--");<br>
	 * result: {"1-2","-3","","4"}<br>
	 * 
	 * @Title: split 高效的字符串分割的方法
	 * @author: lixl  
	 * @date: 2014年4月18日 上午10:31:45 
	 * @param s 需要分割的字符串 
	 * @param delimiter 分割的标示
	 * @return
	 */
	public static String[] split(String s, String delimiter) {
		int delimiterLength;
		int stringLength = s.length();
		if (delimiter == null || (delimiterLength = delimiter.length()) == 0) {
			return new String[] { s };
		}
		int count;
		int start;
		int end;

		// Scan s and count the tokens.
		count = 0;
		start = 0;
		while ((end = s.indexOf(delimiter, start)) != -1) {
			count++;
			start = end + delimiterLength;
		}
		count++;
		String[] result = new String[count];
		count = 0;
		start = 0;
		while ((end = s.indexOf(delimiter, start)) != -1) {
			result[count] = (s.substring(start, end)).trim();
			count++;
			start = end + delimiterLength;
		}
		end = stringLength;
		result[count] = s.substring(start, end).trim();

		return (result);
	}

	/**
	 * 高效的字符串分割的方法,包括分割的字符,并做了trim操作
	 * 如：
	 * StringHelper.split("1-2-3", "-");<br>
	 * result: {"1","-","2","-","3"}<br>
	 * StringHelper.split("-1--2-", "-");<br>
	 * result: {"","-","1","-","","-","2","-",""}<br>
	 * StringHelper.split("123", "");<br>
	 * result: {"123"}<br>
	 * StringHelper.split("1-2--3---4----5", "--");<br>
	 * result: {"1-2","--","3","--","-4","--","","--","5"}<br>
	 * 
	 * @Title: splitIncludeDelimiters 字符串分割的方法
	 * @author: lixl  
	 * @date: 2014年4月18日 上午10:56:15 
	 * @param s
	 * @param delimiter
	 * @return
	 */
	public static String[] splitIncludeDelimiters(String s, String delimiter) {
		int delimiterLength;
		// the next statement has the side effect of throwing a null pointer
		// exception if s is null.
		int stringLength = s.length();
		if (delimiter == null || (delimiterLength = delimiter.length()) == 0) {
			// it is not inherently clear what to do if there is no delimiter
			// On one hand it would make sense to return each character because
			// the null String can be found between each pair of characters in
			// a String. However, it can be found many times there and we don'
			// want to be returning multiple null tokens.
			// returning the whole String will be defined as the correct
			// behavior
			// in this instance.
			return new String[] { s };
		}

		// a two pass solution is used because a one pass solution would
		// require the possible resizing and copying of memory structures
		// In the worst case it would have to be resized n times with each
		// resize having a O(n) copy leading to an O(n^2) algorithm.

		int count;
		int start;
		int end;

		// Scan s and count the tokens.
		count = 0;
		start = 0;
		while ((end = s.indexOf(delimiter, start)) != -1) {
			count += 2;
			start = end + delimiterLength;
		}
		count++;

		// allocate an array to return the tokens,
		// we now know how big it should be
		String[] result = new String[count];

		// Scan s again, but this time pick out the tokens
		count = 0;
		start = 0;
		while ((end = s.indexOf(delimiter, start)) != -1) {
			result[count] = (s.substring(start, end)).trim();
			count++;
			result[count] = delimiter;
			count++;
			start = end + delimiterLength;
		}
		end = stringLength;
		result[count] = s.substring(start, end).trim();

		return (result);
	}

	/**
	 * 把一个字符串数组进行拼接的方法
	 * 如：[a,b,c,d]->abcd
	 * @Title: join
	 * @author: lixl  
	 * @date: 2014年4月18日 上午10:58:16 
	 * @param array 字符串数组
	 * @return 返回拼接之后的字符串
	 */
	public static String join(String[] array) {
		return join(array, "");
	}

	/**
	 * 把一个字符串数组进行拼接的方法
	 * 如：join([a,b,c,d], "-")->a-b-c-d
	 * @Title: join 字符串拼接
	 * @author: lixl  
	 * @date: 2014年4月18日 上午10:59:59 
	 * @param array
	 * @param delimiter
	 * @return
	 */
	public static String join(String[] array, String delimiter) {
		// Cache the length of the delimiter
		// has the side effect of throwing a NullPointerException if
		// the delimiter is null.
		int delimiterLength = delimiter.length();

		// Nothing in the array return empty string
		// has the side effect of throwing a NullPointerException if
		// the array is null.
		if (array.length == 0)
			return "";

		// Only one thing in the array, return it.
		if (array.length == 1) {
			if (array[0] == null)
				return "";
			return array[0];
		}

		// Make a pass through and determine the size
		// of the resulting string.
		int length = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] != null)
				length += array[i].length();
			if (i < array.length - 1)
				length += delimiterLength;
		}

		// Make a second pass through and concatenate everything
		// into a string buffer.
		StringBuffer result = new StringBuffer(length);
		for (int i = 0; i < array.length; i++) {
			if (array[i] != null)
				result.append(array[i]);
			if (i < array.length - 1)
				result.append(delimiter);
		}

		return result.toString();
	}
	/**
	 * 把一个字符串的集合进行拼接的方法
	 * @param list
	 * @param delimiter
	 * @return
	 */
	public static String join(List<String> list ,String delimiter){
		if(list.isEmpty()){
			return "";
		}
		String[] arr = list.toArray(new String[list.size()]);
		if(!StringUtil.isEmpty(delimiter)){
			return join(arr,delimiter);
		}else{
			return join(arr);
		}
		 
	}
	/**
	 * 字符串替换的方法
	 * 如：replace("abadah4a6", "a", "-")-->"-b-d-h4-6"
	 * @Title: replace
	 * @author: lixl  
	 * @date: 2014年4月18日 上午11:01:47 
	 * @param s
	 * @param find 查找的字符串
	 * @param replace 替换的字符串
	 * @return string 返回替换之后的字符串
	 */
	public static String replace(String s, String find, String replace) {
		int findLength;
		// the next statement has the side effect of throwing a null pointer
		// exception if s is null.
		int stringLength = s.length();
		if (find == null || (findLength = find.length()) == 0) {
			// If there is nothing to find, we won't try and find it.
			return s;
		}
		if (replace == null) {
			// a null string and an empty string are the same
			// for replacement purposes.
			replace = "";
		}
		int replaceLength = replace.length();

		// We need to figure out how long our resulting string will be.
		// This is required because without it, the possible resizing
		// and copying of memory structures could lead to an unacceptable
		// runtime.
		// In the worst case it would have to be resized n times with each
		// resize having a O(n) copy leading to an O(n^2) algorithm.
		int length;
		if (findLength == replaceLength) {
			// special case in which we don't need to count the replacements
			// because the count falls out of the length formula.
			length = stringLength;
		} else {
			int count;
			int start;
			int end;

			// Scan s and count the number of times we find our target.
			count = 0;
			start = 0;
			while ((end = s.indexOf(find, start)) != -1) {
				count++;
				start = end + findLength;
			}
			if (count == 0) {
				// special case in which on first pass, we find there is nothing
				// to be replaced. No need to do a second pass or create a
				// string buffer.
				return s;
			}
			length = stringLength - (count * (findLength - replaceLength));
		}

		int start = 0;
		int end = s.indexOf(find, start);
		if (end == -1) {
			// nothing was found in the string to replace.
			// we can get this if the find and replace strings
			// are the same length because we didn't check before.
			// in this case, we will return the original string
			return s;
		}
		// it looks like we actually have something to replace
		// *sigh* allocate memory for it.
		StringBuffer sb = new StringBuffer(length);

		// Scan s and do the replacements
		while (end != -1) {
			sb.append(s.substring(start, end));
			sb.append(replace);
			start = end + findLength;
			end = s.indexOf(find, start);
		}
		end = stringLength;
		sb.append(s.substring(start, end));

		return (sb.toString());
	}

	/**
	 * Replaces characters that may be confused by an SQL
	 * parser with their equivalent escape characters.
	 * <p>
	 * Any data that will be put in an SQL query should
	 * be be escaped.  This is especially important for data
	 * that comes from untrusted sources such as Internet users.
	 * <p>
	 * For example if you had the following SQL query:<br>
	 * <code>"SELECT * FROM addresses WHERE name='" + name + "' AND private='N'"</code><br>
	 * Without this function a user could give <code>" OR 1=1 OR ''='"</code>
	 * as their name causing the query to be:<br>
	 * <code>"SELECT * FROM addresses WHERE name='' OR 1=1 OR ''='' AND private='N'"</code><br>
	 * which will give all addresses, including private ones.<br>
	 * Correct usage would be:<br>
	 * <code>"SELECT * FROM addresses WHERE name='" + StringHelper.escapeSQL(name) + "' AND private='N'"</code><br>
	 * <p>
	 * Another way to avoid this problem is to use a PreparedStatement
	 * with appropriate placeholders.
	 *
	 * @param s String to be escaped
	 * @return escaped String
	 * @throws NullPointerException if s is null.
	 *
	 * @since ostermillerutils 1.00.00
	 */
	/**
	 * 防止sql注入的方法，下面是实例
	 * Replaces characters that may be confused by an SQL
	 * parser with their equivalent escape characters.
	 * <p>
	 * Any data that will be put in an SQL query should
	 * be be escaped.  This is especially important for data
	 * that comes from untrusted sources such as Internet users.
	 * <p>
	 * For example if you had the following SQL query:<br>
	 * <code>"SELECT * FROM addresses WHERE name='" + name + "' AND private='N'"</code><br>
	 * Without this function a user could give <code>" OR 1=1 OR ''='"</code>
	 * as their name causing the query to be:<br>
	 * <code>"SELECT * FROM addresses WHERE name='' OR 1=1 OR ''='' AND private='N'"</code><br>
	 * which will give all addresses, including private ones.<br>
	 * Correct usage would be:<br>
	 * <code>"SELECT * FROM addresses WHERE name='" + StringUtil.escapeSQL(name) + "' AND private='N'"</code><br>
	 * <p>
	 * Another way to avoid this problem is to use a PreparedStatement
	 * with appropriate placeholders.
	 * @Title: escapeSQL 防止sql注入的方法
	 * @author: lixl  
	 * @date: 2014年4月18日 上午11:09:26 
	 * @param s 需要拼接的sql值
	 * @return
	 */
	public static String escapeSQL(String s) {
		int length = s.length();
		int newLength = length;
		// first check for characters that might
		// be dangerous and calculate a length
		// of the string that has escapes.
		for (int i = 0; i < length; i++) {
			char c = s.charAt(i);
			switch (c) {
			case '\\':
			case '\"':
			case '\'':
			case '\0': {
				newLength += 1;
			}
				break;
			}
		}
		if (length == newLength) {
			// nothing to escape in the string
			return s;
		}
		StringBuffer sb = new StringBuffer(newLength);
		for (int i = 0; i < length; i++) {
			char c = s.charAt(i);
			switch (c) {
			case '\\': {
				sb.append("\\\\");
			}
				break;
			case '\"': {
				sb.append("\\\"");
			}
				break;
			case '\'': {
				sb.append("\\\'");
			}
				break;
			case '\0': {
				sb.append("\\0");
			}
				break;
			default: {
				sb.append(c);
			}
			}
		}
		return sb.toString();
	}

	/**
	 * 编码转换 从UTF-8到GBK
	 * @Title: utf2GBK
	 * @author: lixl  
	 * @date: 2014年4月18日 下午3:21:11 
	 * @param strVal 需要转换的字符串
	 * @return String
	 */
	public static String utf2GBK(String strVal) {
		try {
			if (strVal == null) {
				return "";
			} else {
				strVal = strVal.trim();
				strVal = new String(strVal.getBytes("UTF-8"), "GBK");
				return strVal;
			}
		} catch (Exception exp) {
			return "";
		}
	}

	/**
	 * 字符串前后补0
	 * @param str 字符串
	 * @param strLength 补齐的长度
	 * @param isLeft true向左补 false向右补
	 * @return
	 */
	public static String addZeroForNum(String str, int strLength, boolean isLeft) {
	    int strLen = str.length();
	    if (strLen < strLength) {
	        while (strLen < strLength) {
	            StringBuffer sb = new StringBuffer();
	            if (isLeft) {
	            	sb.append("0").append(str);//左补0
	            } else {
	            	sb.append(str).append("0");//右补0
	            }
	            str = sb.toString();
	            strLen = str.length();
	        }
	    }
	    return str;
	}
	
	
	
	/**
	 *  从“GBK”到“ISO8859_1”得到的字符串
	 * @Title: gbk2ISO 字符集编码转换
	 * @author: lixl  
	 * @date: 2014年4月18日 下午3:20:18 
	 * @param strVal
	 * @return string
	 */
	public static String gbk2ISO(String strVal) {
		try {
			if (strVal == null) {
				return "";
			} else {
				strVal = new String(strVal.getBytes("GBK"), "ISO8859_1");
				return strVal;
			}
		} catch (Exception exp) {
			return "";
		}
	}

	/**
	 * 把字符串 的gbk编码转换为utf-8编码
	 * @Title: gbk2UTF8
	 * @author: lixl  
	 * @date: 2014年4月18日 下午3:19:35 
	 * @param strVal
	 * @return
	 */
	public static String gbk2UTF8(String strVal) {
		try {
			if (strVal == null) {
				return "";
			} else {
				strVal = new String(strVal.getBytes("GBK"), "UTF-8");
				return strVal;
			}
		} catch (Exception exp) {
			return "";
		}
	}

	/**
	 * 把iso-8859-1编码，转换为utf-8
	 * @Title: ISO2UTF8
	 * @author: lixl  
	 * @date: 2014年4月18日 下午3:18:57 
	 * @param strVal
	 * @return string
	 */
	public static String ISO2UTF8(String strVal) {
		try {
			if (strVal == null) {
				return "";
			} else {
				strVal = new String(strVal.getBytes("ISO-8859-1"), "UTF-8");
				return strVal;
			}
		} catch (Exception exp) {
			return "";
		}
	}

	/**
	 *  把字符串的字符集从utf-8 转换为ISO-8859-1
	 * @Title: UTF82ISO
	 * @author: lixl  
	 * @date: 2014年4月18日 下午3:18:15 
	 * @param strVal
	 * @return String
	 */
	public static String UTF82ISO(String strVal) {
		try {
			if (strVal == null) {
				return "";
			} else {
				strVal = new String(strVal.getBytes("UTF-8"), "ISO-8859-1");
				return strVal;
			}
		} catch (Exception exp) {
			return "";
		}
	}

	/**
	 * 在0后面补字符串
	 * @Title: zeroPadString
	 * @author: lixl  
	 * @date: 2014年4月18日 下午4:43:23 
	 * @param string 补充的字符串
	 * @param length 补充之后字符串的长度
	 * @return
	 */
	public static final String zeroPadString(String string, int length) {
		if (string == null || string.length() > length) {
			return string;
		} else {
			StringBuffer buf = new StringBuffer(length);
			buf.append(zeroArray, 0, length - string.length()).append(string);
			return buf.toString();
		}
	}
	
	/**
	 * 字符串后面拼接0
	 * @param string
	 * @param length
	 * @return
	 */
	public static final String stringPadZero(String string, int length) {
		if (string == null || string.length() > length) {
			return string;
		} else {
			StringBuffer buf = new StringBuffer(length);
			return string+(buf.append(zeroArray, 0, length - string.length()));
		}
	}

	/**
	 * 在字符Chauncey右侧补充空格
	 * @Title: blankPadString
	 * @author: lixl  
	 * @date: 2014年4月18日 下午4:45:41 
	 * @param string 初始字符串
	 * @param length 补齐后的字符串长度
	 * @return 补充后的字符串
	 */
	public static final String blankPadString(String string, int length) {
		if (string == null || string.length() > length) {
			return string;
		} else {
			StringBuffer buf = new StringBuffer();

			for (int i = 0; i < length - string.getBytes().length; i++) {
				buf.append(' ');
			}
			buf.append(string);
			return buf.toString();
		}
	}

	/**
	 * <p>在字符串右边补充特定字符串</p>    
	 * @param oriString 原始字符串
	 * @param padString 特定字符串
	 * @param times 填充次数
	 * @return 字符串<br><br>
	 * author bgz<BR>
	 * date 2010-1-26<br>
	 * remark <br>
	 */
	/**
	 * 在字符串右边补充特定字符串
	 * @Title: rightPadString
	 * @author: lixl  
	 * @date: 2014年4月18日 下午4:46:13 
	 * @param oriString 原始字符串
	 * @param padString 特定字符串
	 * @param times 填充次数
	 * @return 填充之后的字符串
	 */
	public static String rightPadString(String oriString, String padString,
			int times) {
		StringBuffer buf = new StringBuffer(oriString);

		for (int i = 0; i < times; i++) {
			buf.append(padString);
		}
		return buf.toString();
	}

	/**
	 * 截取字符串的前步
	 * @Title: substringBefore
	 * @author: lixl  
	 * @date: 2014年5月22日 下午4:54:54 
	 * @param str
	 * @param separator
	 * @return
	 */
	public static String substringBefore(String str, String separator) {
		if (isEmpty(str) || separator == null)
			return str;
		if (separator.length() == 0)
			return "";
		int pos = str.indexOf(separator);
		if (pos == -1)
			return str;
		else
			return str.substring(0, pos);
	}

	/**
	 * 截取字符串的后部
	 * @Title: substringAfter
	 * @author: lixl  
	 * @date: 2014年5月22日 下午4:55:17 
	 * @param str
	 * @param separator
	 * @return
	 */
	public static String substringAfter(String str, String separator) {
		if (isEmpty(str))
			return str;
		if (separator == null)
			return "";
		int pos = str.indexOf(separator);
		if (pos == -1)
			return "";
		else
			return str.substring(pos + separator.length());
	}

	/**
	 * 判断字符串为空
	 * @Title: isBlank
	 * @author: lixl  
	 * @date: 2014年5月22日 下午4:55:37 
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return true;
		for (int i = 0; i < strLen; i++)
			if (!Character.isWhitespace(str.charAt(i)))
				return false;

		return true;
	}

	/**
	 * 判断字符串不为空
	 * @Title: isNotBlank
	 * @author: lixl  
	 * @date: 2014年5月22日 下午4:56:03 
	 * @param str
	 * @return
	 */
	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}
	
	
	/**
	 * 判定是否是數字
	 * @param str
	 * @return
	 */
	public static  boolean isNumeric(String str){
		Pattern pattern = Pattern.compile("[0-9]*");
		if(isEmpty(str)||isNull(str)){
			return false;
		}
		Matcher isNum = pattern.matcher(str);
		if( !isNum.matches() )
		{
		return false;
		}
		return true;
	}

	
	/**
	 * 对此类中的方法进行测试
	 * @Title: main
	 * @author: lixl  
	 * @date: 2014年4月16日 下午2:52:44 
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * System.out.println(converterToFirstSpell("河南大学"));
		 * System.out.println(converterToSpell("河南大学"));
		 */
		/*
		 * String str = null; System.out.println(StringBizUtil.isEmpty(str));
		 */
		// System.out.println(replace("abadah4a6", "a", "-"));
		//System.out.println(zeroPadString("a", 4));
		
		/*List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("2");
		list.add("3");
		System.out.println(join(list, ","));*/
		
		System.out.println(fieldToProperty("user_name_test"));
	}

}
